<?php

namespace Wechat\Controller;

class WorkorderController extends CommonController
{

    public function index()
    {

        if (!check_access($this->customer["customer_id"], "0")) {
            $this->error("您没有访问权限！");
            exit();
        }

        $id = $_REQUEST["id"];
        $workorder = D("Workorder")->queryById($id);
        $patientInfo = D("Patient")->queryById($workorder["patient_id"]);
        if($workorder["workorder_type"] == 1){
            $examBasicInfo = D("ExamBasic")->queryById($workorder["exam_basic_id"]);
            $examBasicCPInfo = D("ExamBasicCp")->queryById($workorder["exam_basic_cp_id"]);
            $examMeasureInfo = D("ExamMeasure")->queryById($workorder["exam_measure_id"]);
            $rxInfo = D("Rx")->queryById($workorder["rx_id"]);
        }
        if($workorder["workorder_type"] == 2){
            $examBasicInfo = D("ExamBasicSpine")->queryById($workorder["exam_basic_spine_id"]);
            $examMeasureInfo = D("ExamMeasureSpine")->queryById($workorder["exam_measure_spine_id"]);
        }
        if($workorder["workorder_type"] == 3){
            $examBasicCPInfo = D("ExamBasicCpPlus")->queryById($workorder["exam_basic_cp_id"]);
        }

        $examAnalysisInfo = D("ExamAnalysis")->queryById($workorder["exam_analysis_id"]);
        $sales = D("Employee")->queryById($workorder["sales_id"]);
        $prescriber = D("Employee")->queryById($workorder["prescriber_id"]);
        if($workorder["examinator_id"] == -1){
            $examinator["employee_name"] = "无";
        }else{
            $examinator = D("Employee")->queryById($workorder["examinator_id"]);
        }
        $map['class_name'] = array('like', array('all', 'all'), 'or');
        $formDict = D("FormDict")->queryForList($map);
        $employee = D("Employee")->queryById($this->customer["customer_id"]);
        $job = explode(',', $employee["job"]);
        $this->assign("job", $job);
        $this->assign("customerId", $this->customer["customer_id"]);
        $this->assign("workorder", $workorder);
        $this->assign("patientInfo", $patientInfo);
        $this->assign("examBasicInfo", $examBasicInfo);
        $this->assign("examBasicCPInfo", $examBasicCPInfo);
        $this->assign("examMeasureInfo", $examMeasureInfo);
        $this->assign("rxInfo", $rxInfo);
        $this->assign("examAnalysisInfo", $examAnalysisInfo);
        $this->assign("formDict", $formDict);
        $this->assign("sales", $sales);
        $this->assign("examinator", $examinator);
        $this->assign("prescriber", $prescriber);
        $this->display("index");

    }

    public function patientInfo()
    {
        $patientId = $_REQUEST["patientId"];
        $workorderId = $_REQUEST["workorderId"];

        if (isNullOrEmpty($patientId)) {
            $workorder = D("Workorder")->queryById($workorderId);
            $patientId = $workorder["patient_id"];
        }

        $patientInfo = D("Patient")->queryById($patientId);
        $employee = D("Employee")->queryById($patientInfo["update_id"]);
        $map["class_name"] = array('like', array('patient', 'all'), 'or');
        $formDict = D("FormDict")->queryForList($map);
        $user = D("Employee")->queryById($this->customer["customer_id"]);
        $job = explode(',', $user["job"]);
        $this->assign("job", $job);
        $this->assign("patientInfo", $patientInfo);
        $this->assign("customerId", $this->customer["customer_id"]);
        $this->assign("workorder", $workorder);
        $this->assign("employeeName", $employee["employee_name"]);
        $this->assign("patientId", $patientId);
        $this->assign("workorderId", $workorderId);
        $this->assign("formDict", $formDict);
        $this->display("patientInfo");

    }

    public function checkPatientInfo()
    {
        $map["patient_name"] = I("post.name");
//        $map["id_no"] = I("post.idNo");
        $patientInfo = D("Patient")->queryForList(null,$map);

        if (isNullOrEmpty($patientInfo)) {
            $data["code"] = 0;
        } else {
            $data = $patientInfo;
        }
        echo json_encode($data);

    }

    public function getPatientInfo()
    {
        $map["patient_id"] = I("post.patientId");
        $patientInfo = D("Patient")->queryByMap($map);

        if (isNullOrEmpty($patientInfo)) {
            $data["code"] = 0;
        } else {
            $data = $patientInfo;
            $data["code"] = 200;
        }
        echo json_encode($data);
    }

    public function examBasic()
    {

        $examBasicId = $_REQUEST["examBasicId"];
        $workorderId = $_REQUEST["workorderId"];

        if (isNullOrEmpty($examBasicId)) {
            $workorder = D("Workorder")->queryById($workorderId);
            $examBasicId = $workorder["exam_basic_id"];
        }
        $examBasic = D("ExamBasic")->queryById($examBasicId);

        $employee = D("Employee")->queryById($examBasic["update_id"]);
        $map['class_name'] = array('like', array('exam_basic', 'all'), 'or');
        $formDict = D("FormDict")->queryForList($map);
        $user = D("Employee")->queryById($this->customer["customer_id"]);
        $job = explode(',', $user["job"]);
        $this->assign("job", $job);
        $this->assign("customerId", $this->customer["customer_id"]);
        $this->assign("workorder", $workorder);
        $this->assign("examBasic", $examBasic);
        $this->assign("employeeName", $employee["employee_name"]);
        $this->assign("examBasicId", $examBasicId);
        $this->assign("workorderId", $workorderId);
        $this->assign("formDict", $formDict);
        if($examBasic["version"] == 1){
            $this->display("examBasic");
        }else{
            $this->display("examBasicv2");
        }




    }

    public function examMeasure()
    {

        $examMeasureId = $_REQUEST["examMeasureId"];
        $workorderId = $_REQUEST["workorderId"];

        if (isNullOrEmpty($examMeasureId)) {
            $workorder = D("Workorder")->queryById($workorderId);
            $examMeasureId = $workorder["exam_measure_id"];
        }

        $examMeasure = D("ExamMeasure")->queryById($examMeasureId);
        $employee = D("Employee")->queryById($examMeasure["update_id"]);
        $map['class_name'] = array('like', array('exam_measure', 'all'), 'or');
        $formDict = D("FormDict")->queryForList($map);
        $user = D("Employee")->queryById($this->customer["customer_id"]);
        $job = explode(',', $user["job"]);
        $this->assign("job", $job);
        $this->assign("customerId", $this->customer["customer_id"]);
        $this->assign("workorder", $workorder);
        $this->assign("examMeasure", $examMeasure);
        $this->assign("employeeName", $employee["employee_name"]);
        $this->assign("examMeasureId", $examMeasureId);
        $this->assign("workorderId", $workorderId);
        $this->assign("formDict", $formDict);
        if($examMeasure["version"] == 1){
            $this->display("examMeasure");
        }else{
            $this->display("examMeasurev2");
        }

    }

    public function examBasicCP()
    {
        $examBasicCPId = $_REQUEST["examBasicCPId"];
        $workorderId = $_REQUEST["workorderId"];
        $workorder = D("Workorder")->queryById($workorderId);

        if (isNullOrEmpty($examBasicCPId)) {
            $examBasicCPId = $workorder["exam_basic_cp_id"];
        }
        if($workorder["workorder_type"] == 3){
            $examBasicCP = D("ExamBasicCpPlus")->queryById($examBasicCPId);
        }else{
            $examBasicCP = D("ExamBasicCp")->queryById($examBasicCPId);
        }

        $employee = D("Employee")->queryById($examBasicCP["update_id"]);
        $map['class_name'] = array('like', array('exam_basic_cp', 'all'), 'or');
        $formDict = D("FormDict")->queryForList($map);
        $user = D("Employee")->queryById($this->customer["customer_id"]);
        $job = explode(',', $user["job"]);
        $this->assign("job", $job);
        $this->assign("customerId", $this->customer["customer_id"]);
        $this->assign("workorder", $workorder);
        $this->assign("examBasicCP", $examBasicCP);
        $this->assign("employeeName", $employee["employee_name"]);
        $this->assign("examBasicCPId", $examBasicCPId);
        $this->assign("workorderId", $workorderId);
        $this->assign("formDict", $formDict);
        if($workorder["workorder_type"] == 3){
            $this->display("examBasicCPPlus");
        }else{
            $this->display("examBasicCP");
        }


    }

    public function examAnalysis()
    {

        $examAnalysisId = $_REQUEST["examAnalysisId"];
        $workorderId = $_REQUEST["workorderId"];

        if (isNullOrEmpty($examAnalysisId)) {
            $workorder = D("Workorder")->queryById($workorderId);
            $examAnalysisId = $workorder["exam_analysis_id"];
        }

        $examAnalysis = D("ExamAnalysis")->queryById($examAnalysisId);
        $employee = D("Employee")->queryById($examAnalysis["update_id"]);
        $map['class_name'] = array('like', array('exam_analysis', 'all'), 'or');
        $formDict = D("FormDict")->queryForList($map);
        $user = D("Employee")->queryById($this->customer["customer_id"]);
        $job = explode(',', $user["job"]);
        $this->assign("job", $job);
        $this->assign("customerId", $this->customer["customer_id"]);
        $this->assign("examAnalysis", $examAnalysis);
        $this->assign("employeeName", $employee["employee_name"]);
        $this->assign("examAnalysisId", $examAnalysisId);
        $this->assign("workorderId", $workorderId);
        $this->assign("formDict", $formDict);
        $this->display("examAnalysis");

    }

    public function examBasicSpine()
    {

        $examBasicSpineId = $_REQUEST["examBasicSpineId"];
        $workorderId = $_REQUEST["workorderId"];

        if (isNullOrEmpty($examBasicSpineId)) {
            $workorder = D("Workorder")->queryById($workorderId);
            $examBasicSpineId = $workorder["exam_basic_spine_id"];
        }
        $examBasicSpine = D("ExamBasicSpine")->queryById($examBasicSpineId);

//        $examBasic = D("ExamBasic")->alias("eb")
//            ->join("left join __FORMDICT__ fd on fd.group_id = gm.group_id")
//            ->join("left join __CUSTOMER__ c on c.customer_id = gm.customer_id")
//            ->where($failureGroupCustomerMap)
//            ->field("c.customer_id, c.openid")
//            ->select();


        $employee = D("Employee")->queryById($examBasicSpine["update_id"]);
        $map['class_name'] = array('like', array('exam_basic_spine', 'all'), 'or');
        $formDict = D("FormDict")->queryForList($map);
        $user = D("Employee")->queryById($this->customer["customer_id"]);
        $job = explode(',', $user["job"]);
        $this->assign("job", $job);
        $this->assign("customerId", $this->customer["customer_id"]);
        $this->assign("workorder", $workorder);
        $this->assign("examBasicSpine", $examBasicSpine);
        $this->assign("employeeName", $employee["employee_name"]);
        $this->assign("examBasicSpineId", $examBasicSpineId);
        $this->assign("workorderId", $workorderId);
        $this->assign("formDict", $formDict);
        $this->display("examBasicSpine");

    }

    public function examMeasureSpine()
    {
//        if (!check_access($this->customer["customer_id"], "0", 100)) {
//            $this->error("您没有访问权限！");
//            exit();
//        }

        $examMeasureSpineId = $_REQUEST["examMeasureSpineId"];
        $workorderId = $_REQUEST["workorderId"];

        if (isNullOrEmpty($examMeasureSpineId)) {
            $workorder = D("Workorder")->queryById($workorderId);
            $examMeasureSpineId = $workorder["exam_measure_spine_id"];
        }

        $examMeasureSpine = D("ExamMeasureSpine")->queryById($examMeasureSpineId);
        $employee = D("Employee")->queryById($examMeasureSpine["update_id"]);
        $map['class_name'] = array('like', array('exam_measure_spine', 'all'), 'or');
        $formDict = D("FormDict")->queryForList($map);
        $user = D("Employee")->queryById($this->customer["customer_id"]);
        $job = explode(',', $user["job"]);
        $this->assign("job", $job);
        $this->assign("customerId", $this->customer["customer_id"]);
        $this->assign("workorder", $workorder);
        $this->assign("examMeasureSpine", $examMeasureSpine);
        $this->assign("employeeName", $employee["employee_name"]);
        $this->assign("examMeasureSpineId", $examMeasureSpineId);
        $this->assign("workorderId", $workorderId);
        $this->assign("formDict", $formDict);
        $this->display("examMeasureSpine");

    }

    public function examMeasureSpineDraw()
    {
//        if (!check_access($this->customer["customer_id"], "0", 100)) {
//            $this->error("您没有访问权限！");
//            exit();
//        }

        $examMeasureSpineId = $_REQUEST["examMeasureSpineId"];
        $workorderId = $_REQUEST["workorderId"];

        if (isNullOrEmpty($examMeasureSpineId)) {
            $workorder = D("Workorder")->queryById($workorderId);
            $examMeasureSpineId = $workorder["exam_measure_spine_id"];
        }

        $examMeasureSpine = D("ExamMeasureSpine")->queryById($examMeasureSpineId);
        $employee = D("Employee")->queryById($examMeasureSpine["update_id2"]);
        $map['class_name'] = array('like', array('exam_measure', 'all'), 'or');
        $formDict = D("FormDict")->queryForList($map);
        $user = D("Employee")->queryById($this->customer["customer_id"]);
        $job = explode(',', $user["job"]);
        $this->assign("job", $job);
        $this->assign("customerId", $this->customer["customer_id"]);
        $this->assign("workorder", $workorder);
        $this->assign("examMeasureSpine", $examMeasureSpine);
        $this->assign("employeeName", $employee["employee_name"]);
        $this->assign("examMeasureSpineId", $examMeasureSpineId);
        $this->assign("workorderId", $workorderId);
        $this->assign("formDict", $formDict);
        $this->display("examMeasureSpineDraw");

    }


    public function workorderList()
    {
        $status = I("post.status");
        $startdate = I("post.startdate");
        $enddate = I("post.enddate");
        $map["wo.workorder_type"] = I("post.workorderType");
        if (!isNullOrEmpty($status)) {
            $map["wo.status"] = $status;
        }

        $employee = D("Employee")->queryById($this->customer["customer_id"]);
        $job = explode(',', $employee["job"]);

        $where["wo.creator_id"] = $this->customer["customer_id"];
        if ($status == -1) {
//            $map2["wo.creator_id"] = $this->customer["customer_id"];
        }

        if ($job[0] == 100) {
            $all = true;
        }
        if ($status == 1) {
            if ($job[0] == 3) {
                $all = true;
            } else {
                $where["wo.sales_id"] = $this->customer["customer_id"];
                $where['_logic'] = 'or';
            }
        }
        if ($status == 2) {
            if ($job[0] == 3 || $job[0] == 4) {
                $all = true;
            } else {
                $where["wo.sales_id"] = $this->customer["customer_id"];
                $where["wo.examinator_id"] = $this->customer["customer_id"];
                $where['_logic'] = 'or';
            }
//        $where['title']  = array('like','%thinkphp%');

        }
        if ($status == 3 || $status == 4) {
            if ($job[0] == 3 || $job[0] == 4 || $job[2] == 2) {
                $all = true;
            } else {
                $where["wo.sales_id"] = $this->customer["customer_id"];
                $where["wo.examinator_id"] = $this->customer["customer_id"];
                $where['_logic'] = 'or';
            }
//        $where['title']  = array('like','%thinkphp%');

        }
        if ($status == 0) {
            if ($job[0] == 3 || $job[0] == 4 || $job[2] == 2) {
                $all = true;
            } else {
                $where["wo.sales_id"] = $this->customer["customer_id"];
                $where["wo.examinator_id"] = $this->customer["customer_id"];
                $where['_logic'] = 'or';
            }
//        $where['title']  = array('like','%thinkphp%');

        }

        if ($all != true){
            $map2["_complex"] = $where;
        }

//        if($status == 1){
//            $map2["wo.creator_id"] = $this->customer["customer_id"];
//        }else{
//            if ($job[4] == 1) {
//                $map2["wo.sales_id"] = $this->customer["customer_id"];
////              $map2['_logic'] = 'OR';
//            } else {
//                $map2 = null;
//            }
//        }

        if (!isNullOrEmpty($startdate)) {
            $map["wo#create_time-egt"] = $startdate;
        }
        if (!isNullOrEmpty($enddate)) {
            $map["wo#create_time-elt"] = $enddate . " 23:59:59";
        }
//
//        $list = D("Order")->alias("o")
//            ->join("left join __PRODUCT__ p on p.product_id = o.product_id")
//            ->where($map)
//            ->order("o.order_id desc")
//            ->field("o.*,p.price as single_price,p.image_url,p.product_name")
//            ->select();

        $workOrderList = D("Workorder")->queryForList(null, $map, $map2);
        echo json_encode($workOrderList);
    }

    /**
     * 保存患者基本资料
     */
    public function patientInfoPost()
    {
        $workorder = D("Workorder")->queryById(I("post.workorderId"));
        $patientId = I("post.patientId");
        if (isNullOrEmpty($patientId)) {
            $patientId = D('Patient')->add(array(
                'completed' => -1,
                'create_time' => getCurrentDate(),
                'update_time' => getCurrentDate(),
                'update_id' => $this->customer["customer_id"],
            ));
        }
        $patientInfo = D("Patient")->queryById($patientId);

        $patientInfo["patient_name"] = I("post.name");
        $patientInfo["sex"] = I("post.sex");
        $patientInfo["age"] = I("post.age");
        $patientInfo["height"] = I("post.height");
        $patientInfo["weight"] = I("post.weight");
        $patientInfo["diagnosis"] = I("post.diagnosis");
        $patientInfo["happen_date"] = I("post.happenDate");
        $patientInfo["id_no"] = I("post.idNo");
        $patientInfo["address"] = I("post.address");
        $patientInfo["contact"] = I("post.contact");
        $patientInfo["mobile"] = I("post.mobile");
        $patientInfo["hospital"] = I("post.hospital");
        $patientInfo["doctor"] = I("post.doctor");

        $patientInfo["reason"] = I("post.reason");
        $patientInfo["compl"] = I("post.compl");
        $patientInfo["allergy"] = I("post.allergy");
        $patientInfo["old_orth"] = I("post.oldOrth");
        $patientInfo["orth_part"] = I("post.orthPart");
        $patientInfo["history"] = I("post.history");
        $patientInfo["diagnosis1"] = I("post.diagnosis1");
        $patientInfo["diagnosis2"] = I("post.diagnosis2");
        $patientInfo["diagnosis3"] = I("post.diagnosis3");
        $patientInfo["diagnosis4"] = I("post.diagnosis4");
        $patientInfo["diagnosis5"] = I("post.diagnosis5");
        $patientInfo["update_time"] = getCurrentDate();
        $patientInfo["update_id"] = $this->customer["customer_id"];
        $patientInfo["completed"] = I("post.completed");
        D("Patient")->updateInfo($patientInfo);

        $workorder["patient_id"] = $patientId;
        D("Workorder")->updateInfo($workorder);
        $data["code"] = 200;
        echo json_encode($data);
    }

    public function examBasicPost()
    {
        $workorder = D("Workorder")->queryById(I("post.workorderId"));
        $examBasicId = I("post.examBasicId");
        if (isNullOrEmpty($examBasicId)) {
            $examBasicId = D('ExamBasic')->add(array(
                'completed' => -1,
                'version' => 2,
                'create_time' => getCurrentDate(),
                'update_time' => getCurrentDate(),
                'update_id' => $this->customer["customer_id"],
            ));
        }
        $examBasicInfo = D("ExamBasic")->queryById($examBasicId);

        $examBasicInfo["update_time"] = getCurrentDate();
        $examBasicInfo["update_id"] = $this->customer["customer_id"];
        $examBasicInfo["completed"] = I("post.completed");
        $examMeasureInfo["version"] = 2;

        $examBasicInfo["side"] = I("side");
        $workorder["side"] = I("side");
        $examBasicInfo["mobility"] = I("post.mobility");
        $examBasicInfo["shankcontrol"] = I("post.shankcontrol");
        $examBasicInfo["gait"] = I("post.gait");
        $examBasicInfo["notegait"] = I("post.notegait");
        $examBasicInfo["footarch"] = I("post.footarch");
        $examBasicInfo["notefootarch"] = I("post.notefootarch");
        $examBasicInfo["toe"] = I("post.toe");
        $examBasicInfo["notetoe"] = I("post.notetoe");
        $examBasicInfo["forefoot"] = I("post.forefoot");
        $examBasicInfo["complforefoot"] = I("post.complforefoot");
        $examBasicInfo["noteforefoot"] = I("post.noteforefoot");
        $examBasicInfo["heela"] = I("post.heela");
        $examBasicInfo["heelb"] = I("post.heelb");
        $examBasicInfo["heelaangle"] = I("post.heelaangle");
        $examBasicInfo["heelbangle"] = I("post.heelbangle");
        $examBasicInfo["complheel"] = I("post.complheel");
        $examBasicInfo["noteheel"] = I("post.noteheel");
        $examBasicInfo["height_heel"] = I("post.heightHeel");
        $examBasicInfo["shoe_type"] = I("post.shoeType");
        $examBasicInfo["shoe_size"] = I("post.shoeSize");
        $examBasicInfo["burden"] = I("post.burden");
        $examBasicInfo["burdenfoot"] = I("post.burdenfoot");
        $examBasicInfo["discomfort"] = I("post.discomfort");
        $examBasicInfo["anklesymmetry"] = I("post.anklesymmetry");
        $examBasicInfo["dysfunction"] = I("post.dysfunction");
        $examBasicInfo["takephoto"] = I("post.takephoto");
        $examBasicInfo["takevideo"] = I("post.takevideo");

        D("ExamBasic")->updateInfo($examBasicInfo);
        $workorder["exam_basic_id"] = $examBasicId;
        D("Workorder")->updateInfo($workorder);
        $data["code"] = 200;
//        $data["alert"] = "$examBasicId".$examBasicId;
        echo json_encode($data);
    }

    public function examMeasurePost()
    {
        $workorder = D("Workorder")->queryById(I("post.workorderId"));
        $examMeasureId = I("post.examMeasureId");
        if (isNullOrEmpty($examMeasureId)) {
            $examMeasureId = D('ExamMeasure')->add(array(
                'completed' => -1,
                'version' => 2,
                'create_time' => getCurrentDate(),
                'update_time' => getCurrentDate(),
                'update_id' => $this->customer["customer_id"],
            ));
        }
        $examMeasureInfo = D("ExamMeasure")->queryById($examMeasureId);

        $examMeasureInfo["update_time"] = getCurrentDate();
        $examMeasureInfo["update_id"] = $this->customer["customer_id"];
        $examMeasureInfo["completed"] = I("post.completed");
        $examMeasureInfo["version"] = 2;

        $examMeasureInfo["burden"] = I("post.burden");
        $examMeasureInfo["shank_0"] = I("post.shank0");
        $examMeasureInfo["shank_1"] = I("post.shank1");
        $examMeasureInfo["shank_2"] = I("post.shank2");
        $examMeasureInfo["shank_3"] = I("post.shank3");
        $examMeasureInfo["shank_4"] = I("post.shank4");
        $examMeasureInfo["shank_5"] = I("post.shank5");
        $examMeasureInfo["shank_6"] = I("post.shank6");
        $examMeasureInfo["shank_7"] = I("post.shank7");
        $examMeasureInfo["shank_8"] = I("post.shank8");
        $examMeasureInfo["shank_9"] = I("post.shank9");
        $examMeasureInfo["shank_10"] = I("post.shank10");
        $examMeasureInfo["shank_11"] = I("post.shank11");
        $examMeasureInfo["shank_12"] = I("post.shank12");
        $examMeasureInfo["shank_13"] = I("post.shank13");
        $examMeasureInfo["shank_14"] = I("post.shank14");
        $examMeasureInfo["shank_15"] = I("post.shank15");
//        $examMeasureInfo["shank_16"] = I("post.shank16");
 //       $examMeasureInfo["shank_17"] = I("post.shank17");
  //      $examMeasureInfo["shank_18"] = I("post.shank18");
 //       $examMeasureInfo["shank_19"] = I("post.shank19");
  //      $examMeasureInfo["shank_20"] = I("post.shank20");

        D("ExamMeasure")->updateInfo($examMeasureInfo);
        $workorder["exam_measure_id"] = $examMeasureId;
        D("Workorder")->updateInfo($workorder);
        $data["code"] = 200;
//        $data["alert"] = "mobility".$examMeasureInfo["mobility"];
        echo json_encode($data);
    }

    public function examBasicCPPost()
    {
        $workorder = D("Workorder")->queryById(I("post.workorderId"));
        $examBasicCPId = I("post.examBasicCPId");

        if($workorder["workorder_type"] == 3){
            if (isNullOrEmpty($examBasicCPId)) {
                $examBasicCPId = D('ExamBasicCpPlus')->add(array(
                    'completed' => -1,
                    'create_time' => getCurrentDate(),
                    'update_time' => getCurrentDate(),
                    'update_id' => $this->customer["customer_id"],
                ));
            }
            $examBasicCPInfo = D("ExamBasicCpPlus")->queryById($examBasicCPId);

            $examBasicCPInfo["update_time"] = getCurrentDate();
            $examBasicCPInfo["update_id"] = $this->customer["customer_id"];
            $examBasicCPInfo["completed"] = I("post.completed");
            for($i = 1; $i < 43; $i++){
                $examBasicCPInfo["a".$i."l"] = I("post.a".$i."l");
                $examBasicCPInfo["a".$i."r"] = I("post.a".$i."r");
            }

            $examBasicCPInfo["a43"] = I("post.a43");
            $examBasicCPInfo["a44"] = I("post.a44");
            $examBasicCPInfo["a45"] = I("post.a45");
            $examBasicCPInfo["a46"] = I("post.a46");
            $examBasicCPInfo["note"] = I("post.note");

            D("ExamBasicCpPlus")->updateInfo($examBasicCPInfo);
            $workorder["exam_basic_cp_id"] = $examBasicCPId;
            D("Workorder")->updateInfo($workorder);
            $data["code"] = 200;
//        $data["alert"] = "workorder".$workorder["exam_basic_cp_id"];
            echo json_encode($data);
        }
        else{
            if (isNullOrEmpty($examBasicCPId)) {
                $examBasicCPId = D('ExamBasicCp')->add(array(
                    'completed' => -1,
                    'create_time' => getCurrentDate(),
                    'update_time' => getCurrentDate(),
                    'update_id' => $this->customer["customer_id"],
                ));
            }
            $examBasicCPInfo = D("ExamBasicCp")->queryById($examBasicCPId);

            $examBasicCPInfo["update_time"] = getCurrentDate();
            $examBasicCPInfo["update_id"] = $this->customer["customer_id"];
            $examBasicCPInfo["completed"] = I("post.completed");
            $examBasicCPInfo["version"] = 2;

            $examBasicCPInfo["ms01"] = I("post.ms01");
            $examBasicCPInfo["ms02"] = I("post.ms02");
            $examBasicCPInfo["ms03"] = I("post.ms03");
            $examBasicCPInfo["ms04"] = I("post.ms04");
            $examBasicCPInfo["ms05"] = I("post.ms05");
            $examBasicCPInfo["ms06"] = I("post.ms06");
            $examBasicCPInfo["ms07"] = I("post.ms07");
            $examBasicCPInfo["ms08"] = I("post.ms08");
            $examBasicCPInfo["ms09"] = I("post.ms09");
            $examBasicCPInfo["ms10"] = I("post.ms10");
            $examBasicCPInfo["mt01"] = I("post.mt01");
            $examBasicCPInfo["mt02"] = I("post.mt02");
            $examBasicCPInfo["mt03"] = I("post.mt03");
            $examBasicCPInfo["mt04"] = I("post.mt04");
            $examBasicCPInfo["mt05"] = I("post.mt05");
            $examBasicCPInfo["mt06"] = I("post.mt06");
            $examBasicCPInfo["mt07"] = I("post.mt07");
            $examBasicCPInfo["mt08"] = I("post.mt08");
            $examBasicCPInfo["mt09"] = I("post.mt09");
            $examBasicCPInfo["mt10"] = I("post.mt10");
            $examBasicCPInfo["mt11"] = I("post.mt11");
            $examBasicCPInfo["mt12"] = I("post.mt12");
            $examBasicCPInfo["mt13"] = I("post.mt13");
            $examBasicCPInfo["mt14"] = I("post.mt14");
            $examBasicCPInfo["jr01"] = I("post.jr01");
            $examBasicCPInfo["jr02"] = I("post.jr02");
            $examBasicCPInfo["jr03"] = I("post.jr03");
            $examBasicCPInfo["jr04"] = I("post.jr04");
            $examBasicCPInfo["jr05"] = I("post.jr05");
            $examBasicCPInfo["jr06"] = I("post.jr06");
            $examBasicCPInfo["jr07"] = I("post.jr07");
            $examBasicCPInfo["jr08"] = I("post.jr08");
            $examBasicCPInfo["jr09"] = I("post.jr09");
            $examBasicCPInfo["jr10"] = I("post.jr10");
            $examBasicCPInfo["jr11"] = I("post.jr11");
            $examBasicCPInfo["jr12"] = I("post.jr12");
            $examBasicCPInfo["sk01"] = I("post.sk01");
            $examBasicCPInfo["sk02"] = I("post.sk02");
            $examBasicCPInfo["fp01"] = I("post.fp01");
            $examBasicCPInfo["b01"] = I("post.b01");
            $examBasicCPInfo["b02"] = I("post.b02");
            $examBasicCPInfo["b03"] = I("post.b03");
            $examBasicCPInfo["b04"] = I("post.b04");

            D("ExamBasicCp")->updateInfo($examBasicCPInfo);
            $workorder["exam_basic_cp_id"] = $examBasicCPId;
            D("Workorder")->updateInfo($workorder);
            $data["code"] = 200;
//        $data["alert"] = "workorder".$workorder["exam_basic_cp_id"];
            echo json_encode($data);
        }
    }


    public function examAnalysisPost()
    {
        $workorder = D("Workorder")->queryById(I("post.workorderId"));
        $examAnalysisId = I("post.examAnalysisId");
        if (isNullOrEmpty($examAnalysisId)) {
            $examAnalysisId = D('ExamAnalysis')->add(array(
                'completed' => -1,
                'create_time' => getCurrentDate(),
                'update_time' => getCurrentDate(),
                'update_id' => $this->customer["customer_id"],
            ));
        }
        $examAnalysisInfo = D("ExamAnalysis")->queryById($examAnalysisId);

        $examAnalysisInfo["update_time"] = getCurrentDate();
        $examAnalysisInfo["update_id"] = $this->customer["customer_id"];
        $examAnalysisInfo["completed"] = I("post.completed");
        $examAnalysisInfo["shank_control"] = I("post.shankControl");
        $examAnalysisInfo["shankcontrol_a"] = I("post.shankcontrolA");
        $examAnalysisInfo["shankcontrol_a_inside"] = I("post.shankcontrolAInside");
        $examAnalysisInfo["shankcontrol_b"] = I("post.shankcontrolB");
        $examAnalysisInfo["shankcontrol_c"] = I("post.shankcontrolC");
        $examAnalysisInfo["ankle_type"] = I("post.ankleType");
        $examAnalysisInfo["ankle_type_other"] = I("post.ankleTypeOther");
        $examAnalysisInfo["ankle_size"] = I("post.ankleSize");
        $examAnalysisInfo["ankle_activity"] = I("post.ankleActivity");
        $examAnalysisInfo["ankle_activity_option"] = I("post.ankleActivityOption");
        $examAnalysisInfo["ankle_activity_other"] = I("post.ankleActivityOther");

        D("ExamAnalysis")->updateInfo($examAnalysisInfo);
        $workorder["exam_analysis_id"] = $examAnalysisId;
        D("Workorder")->updateInfo($workorder);
        $data["code"] = 200;
//        $data["alert"] = "shankcontrol_c".$examAnalysisInfo["shankcontrol_c"];
        echo json_encode($data);
    }

    public function examBasicSpinePost()
    {
        $workorder = D("Workorder")->queryById(I("post.workorderId"));
        $examBasicSpineId = I("post.examBasicSpineId");
        if (isNullOrEmpty($examBasicSpineId)) {
            $examBasicSpineId = D('ExamBasicSpine')->add(array(
                'completed' => -1,
                'create_time' => getCurrentDate(),
                'update_time' => getCurrentDate(),
                'update_id' => $this->customer["customer_id"],
            ));
        }
        $examBasicSpineInfo = D("ExamBasicSpine")->queryById($examBasicSpineId);

        $examBasicSpineInfo["update_time"] = getCurrentDate();
        $examBasicSpineInfo["update_id"] = $this->customer["customer_id"];
        $examBasicSpineInfo["completed"] = I("post.completed");
        $examBasicSpineInfo["mobility"] = I("post.mobility");
        $examBasicSpineInfo["stand"] = I("post.stand");
        $examBasicSpineInfo["stand_describe"] = I("post.standDescribe");
        $examBasicSpineInfo["sit"] = I("post.sit");
        $examBasicSpineInfo["sit_describe"] = I("post.sitDescribe");
        $examBasicSpineInfo["endurance"] = I("post.endurance");
        $examBasicSpineInfo["endurance_time"] = I("post.enduranceTime");
        $examBasicSpineInfo["structure"] = I("post.structure");
        $examBasicSpineInfo["broken"] = I("post.broken");
        $examBasicSpineInfo["broken_part"] = I("post.brokenPart");
        $examBasicSpineInfo["bone_quality"] = I("post.boneQuality");
        $examBasicSpineInfo["spine_gap"] = I("post.spineGap");
        $examBasicSpineInfo["feeling"] = I("post.feeling");
        $examBasicSpineInfo["nofeeling"] = I("post.nofeeling");
        $examBasicSpineInfo["nofeeling_part"] = I("post.nofeelingPart");
        $examBasicSpineInfo["pain"] = I("post.pain");
        $examBasicSpineInfo["pain_part"] = I("post.painPart");

        D("ExamBasicSpine")->updateInfo($examBasicSpineInfo);
        $workorder["exam_basic_spine_id"] = $examBasicSpineId;
        D("Workorder")->updateInfo($workorder);
        $data["code"] = 200;
//        $data["alert"] = "$examBasicId".$examBasicId;
        echo json_encode($data);
    }

    public function examMeasureSpinePost()
    {
        $workorder = D("Workorder")->queryById(I("post.workorderId"));
        $examMeasureSpineId = I("post.examMeasureSpineId");
        if (isNullOrEmpty($examMeasureSpineId)) {
            $examMeasureSpineId = D('ExamMeasureSpine')->add(array(
                'completed' => -1,
                'create_time' => getCurrentDate(),
                'update_time' => getCurrentDate(),
                'update_id' => $this->customer["customer_id"],
            ));
        }
        $examMeasureSpineInfo = D("ExamMeasureSpine")->queryById($examMeasureSpineId);

        $examMeasureSpineInfo["update_time"] = getCurrentDate();
        $examMeasureSpineInfo["update_id"] = $this->customer["customer_id"];
        $examMeasureSpineInfo["completed"] = I("post.completed");
        $examMeasureSpineInfo["spine0"] = I("post.spine0");
        $examMeasureSpineInfo["spine1"] = I("post.spine1");
        $examMeasureSpineInfo["spine2"] = I("post.spine2");
        $examMeasureSpineInfo["spine3"] = I("post.spine3");
        $examMeasureSpineInfo["spine4"] = I("post.spine4");
        $examMeasureSpineInfo["spine5"] = I("post.spine5");
        $examMeasureSpineInfo["spine6"] = I("post.spine6");
        $examMeasureSpineInfo["spine7"] = I("post.spine7");
        $examMeasureSpineInfo["spine8"] = I("post.spine8");
        $examMeasureSpineInfo["spine9"] = I("post.spine9");
        $examMeasureSpineInfo["spine10"] = I("post.spine10");
        $examMeasureSpineInfo["spine11"] = I("post.spine11");
        $examMeasureSpineInfo["spine12"] = I("post.spine12");
        $examMeasureSpineInfo["spine13"] = I("post.spine13");
        $examMeasureSpineInfo["spine14"] = I("post.spine14");
        $examMeasureSpineInfo["spine15"] = I("post.spine15");
        $examMeasureSpineInfo["spine16"] = I("post.spine16");
        $examMeasureSpineInfo["spine17"] = I("post.spine17");
        $examMeasureSpineInfo["spine18"] = I("post.spine18");
        $examMeasureSpineInfo["spine19"] = I("post.spine19");
        $examMeasureSpineInfo["spine20"] = I("post.spine20");
        $examMeasureSpineInfo["abdomen"] = I("post.abdomen");
        $examMeasureSpineInfo["oxter"] = I("post.oxter");
        $examMeasureSpineInfo["xiphoid"] = I("post.xiphoid");
        $examMeasureSpineInfo["iliopsoas"] = I("post.iliopsoas");
        $examMeasureSpineInfo["anterior"] = I("post.anterior");
        $examMeasureSpineInfo["trochanter"] = I("post.trochanter");
        $examMeasureSpineInfo["vertex"] = I("post.vertex");
        $examMeasureSpineInfo["sit_height"] = I("post.sitHeight");
        $examMeasureSpineInfo["stand_height"] = I("post.standHeight");
        $examMeasureSpineInfo["pelvic"] = I("post.pelvic");
        $examMeasureSpineInfo["shoulder"] = I("post.shoulder");
        $examMeasureSpineInfo["cobb"] = I("post.cobb");
        $examMeasureSpineInfo["rotate"] = I("post.rotate");


        D("ExamMeasureSpine")->updateInfo($examMeasureSpineInfo);
        $workorder["exam_measure_spine_id"] = $examMeasureSpineId;
        D("Workorder")->updateInfo($workorder);
        $data["code"] = 200;
//        $data["alert"] = "mobility".$examMeasureSpineInfo["mobility"];
        echo json_encode($data);
    }

    public function examMeasureSpineDrawPost()
    {
        $workorder = D("Workorder")->queryById(I("post.workorderId"));
        $examMeasureSpineId = I("post.examMeasureSpineId");
        if (isNullOrEmpty($examMeasureSpineId)) {
            $examMeasureSpineId = D('ExamMeasureSpine')->add(array(
                'completed2' => -1,
                'create_time' => getCurrentDate(),
                'update_time2' => getCurrentDate(),
                'update_id2' => $this->customer["customer_id"],
            ));
        }
        $examMeasureSpineInfo = D("ExamMeasureSpine")->queryById($examMeasureSpineId);

        $examMeasureSpineInfo["update_time2"] = getCurrentDate();
        $examMeasureSpineInfo["update_id2"] = $this->customer["customer_id"];
        $examMeasureSpineInfo["completed2"] = I("post.completed");
        $examMeasureSpineInfo["chest1"] = I("post.chest1");
        $examMeasureSpineInfo["chest2"] = I("post.chest2");
        $examMeasureSpineInfo["chest3"] = I("post.chest3");
        $examMeasureSpineInfo["chest4"] = I("post.chest4");

        D("ExamMeasureSpine")->updateInfo($examMeasureSpineInfo);
        $workorder["exam_measure_spine_id"] = $examMeasureSpineId;
        D("Workorder")->updateInfo($workorder);
        $data["code"] = 200;
//        $data["alert"] = "mobility".$examMeasureSpineInfo["mobility"];
        echo json_encode($data);
    }


/*
    public function createOrder()
    {
        $workorder = D("Workorder")->queryById(I("post.workorderId"));
        $workorder["sales_id"] = I("post.salesId");
        $workorder["update_time"] = getCurrentDate();
//        $workorder["examinator_id"] = $this->customer["customer_id"];
//        $workorder["patient_id"] = I("post.patientId");
//        $workorder["exam_basic_id"] = I("post.examBasicId");
//        $workorder["exam_measure_id"] = I("post.examMeasureId");
//        $workorder["status"] = 1;

        D("Workorder")->updateInfo($workorder);
        $data["code"] = 200;
//        $data["alert"] = "mobility".$examBasicInfo["mobility"];

        //推送信息到接单人
        $processor = D("Employee")->queryById($this->customer["customer_id"]);
        $sales = D("Employee")->queryById($workorder["sales_id"]);
        $patient = D("Patient")->queryById($workorder["patient_id"]);
        $employeeList = D("Employee")->alias("e")
            ->join("left join __CUSTOMER__ c on c.customer_id = e.employee_id")
            ->field("e.*, c.openid")
            ->select();
        $openidArray = array();
        foreach ($employeeList as $singleEmployee) {
            $job = explode(',', $singleEmployee["job"]);
            if ($job[0] == "3" || $job[0] == "100") {//客服
                array_push($openidArray, $singleEmployee["openid"]);
            }
            if ($singleEmployee["employee_id"] == $workorder["sales_id"] && !in_array($singleEmployee["openid"], $openidArray)) {
                array_push($openidArray, $singleEmployee["openid"]);
            }
        }
        foreach ($openidArray as $openid) {
            $notifyData["openid"] = $openid;
            $notifyData["url"] = "http://xwkang.com/wechat/workorder/index/id/" . $workorder["workorder_id"];
            $notifyData["first"] = "收到新的工单：" . $workorder["workorder_no"];
            $notifyData["keyword1"] = "[诊断--" . $patient["diagnosis"] . "]";
            $notifyData["keyword2"] = $workorder["update_time"];
            $notifyData["keyword3"] = $processor["employee_name"];
            $notifyData["remark"] = "患者基本信息：" . $patient["patient_name"] . "，" . $patient["sex"] . "，" . $patient["age"] . "岁。 销售：" . $sales["employee_name"];
            WorkorderCreateNotify($notifyData);
        }

        echo json_encode($data);
    }
*/
    public function createOrder()
    {
        $workorder = D("Workorder")->queryById(I("post.workorderId"));
        $workorder["sales_id"] = I("post.salesId");
        $workorder["update_time"] = getCurrentDate();
//        $workorder["examinator_id"] = $this->customer["customer_id"];
//        $workorder["patient_id"] = I("post.patientId");
//        $workorder["exam_basic_id"] = I("post.examBasicId");
//        $workorder["exam_measure_id"] = I("post.examMeasureId");
        if(I("post.examinatorId") == -1){
            $workorder["examinator_id"] = $workorder["creator_id"];
        }else{
            $workorder["examinator_id"] = I("post.examinatorId");
        }

        if(I("post.prescriberId") == -1){
            $workorder["prescriber_id"] = $workorder["creator_id"];
        }else{
            $workorder["prescriber_id"] = I("post.prescriberId");
        }
        $workorder["dispatch_time"] = getCurrentDate();//new
        $workorder["exam_time"] = getCurrentDate();
        $workorder["take_time"] = getCurrentDate();
        if($workorder["workorder_type"] == 3){
            $workorder["status"] = 4;//new
        }else{
//            $workorder["status"] = 1;//new
            $workorder["status"] = 1;//new
        }
        $workorder["type_name"] = I("post.typeName");
        $workorder["price"] = I("post.price");
        $workorder["deposit"] = I("post.deposit");
        D("Workorder")->updateInfo($workorder);
        $data["code"] = 200;
        //检查表不推送消息
        if($workorder["workorder_type"] == 3) {
            //推送信息到接单人
            $processor = D("Employee")->queryById($this->customer["customer_id"]);
            $patient = D("Patient")->queryById($workorder["patient_id"]);
            $sales = D("Employee")->queryById($workorder["sales_id"]);
            $employeeList = D("Employee")->alias("e")
                ->join("left join __CUSTOMER__ c on c.customer_id = e.employee_id")
                ->field("e.*, c.openid")
                ->select();
            $openidArray = array();
            foreach ($employeeList as $singleEmployee) {
                $job = explode(',', $singleEmployee["job"]);
                if ($job[0] == "100") {//临床部
                    array_push($openidArray, $singleEmployee["openid"]);
                }
                if ($singleEmployee["employee_id"] == $workorder["creator_id"] && !in_array($singleEmployee["openid"], $openidArray)) {
                    array_push($openidArray, $singleEmployee["openid"]);
                }
            }
            foreach ($openidArray as $openid) {
                $notifyData["openid"] = $openid;
                $notifyData["url"] = "http://xwkang.com/wechat/workorder/index/id/" . $workorder["workorder_id"];
                $notifyData["first"] = "已提交新的物理检查表：" . $workorder["workorder_no"];
                $notifyData["keyword1"] = "[诊断--" . $patient["diagnosis"] . "]";
                $notifyData["keyword2"] = $workorder["update_time"];
                $notifyData["keyword3"] = $processor["employee_name"];
                $notifyData["remark"] = "患者基本信息：" . $patient["patient_name"] . "，" . $patient["sex"] . "，" . $patient["age"] . "岁。 销售：" . $sales["employee_name"];
                WorkorderCreateNotify($notifyData);
            }
            echo json_encode($data);
            return;
        }
//        $data["alert"] = "mobility".$examBasicInfo["mobility"];

        //推送信息到接单人
        $processor = D("Employee")->queryById($this->customer["customer_id"]);
        $sales = D("Employee")->queryById($workorder["sales_id"]);
        $prescriber = D("Employee")->queryById($workorder["prescriber_id"]);
        $patient = D("Patient")->queryById($workorder["patient_id"]);
        $employeeList = D("Employee")->alias("e")
            ->join("left join __CUSTOMER__ c on c.customer_id = e.employee_id")
            ->field("e.*, c.openid")
            ->select();
        $openidArray = array();
        foreach ($employeeList as $singleEmployee) {
            $job = explode(',', $singleEmployee["job"]);
            if ($job[1] == "2" || $job[0] == "100") {//临床部
                array_push($openidArray, $singleEmployee["openid"]);
            }
            if ($singleEmployee["employee_id"] == $workorder["prescriber_id"] && !in_array($singleEmployee["openid"], $openidArray)) {
                array_push($openidArray, $singleEmployee["openid"]);
            }
            if ($singleEmployee["employee_id"] == $workorder["sales_id"] && !in_array($singleEmployee["openid"], $openidArray)) {
                array_push($openidArray, $singleEmployee["openid"]);
            }
        }
        foreach ($openidArray as $openid) {
            $notifyData["openid"] = $openid;
            $notifyData["url"] = "http://xwkang.com/wechat/workorder/index/id/" . $workorder["workorder_id"];
            $notifyData["first"] = "收到新的工单：" . $workorder["workorder_no"] . "。请 ".$prescriber["employee_name"]." 登录处方系统出处方。";
            $notifyData["keyword1"] = "[诊断--" . $patient["diagnosis"] . "]";
            $notifyData["keyword2"] = $workorder["update_time"];
            $notifyData["keyword3"] = $processor["employee_name"];
            $notifyData["remark"] = "患者基本信息：" . $patient["patient_name"] . "，" . $patient["sex"] . "，" . $patient["age"] . "岁。 销售：" . $sales["employee_name"];
            WorkorderCreateNotify($notifyData);
        }

        echo json_encode($data);
    }
    //略过此功能
    public function dispatchOrder()
    {
        $workorder = D("Workorder")->queryById(I("post.workorderId"));
        $workorder["dispatch_time"] = getCurrentDate();

        $workorder["status"] = 2;

        D("Workorder")->updateInfo($workorder);
        $data["code"] = 200;
//        $data["alert"] = "mobility".$examBasicInfo["mobility"];

        //推送信息到接单人
        $processor = D("Employee")->queryById($this->customer["customer_id"]);
        $sales = D("Employee")->queryById($workorder["sales_id"]);
        $patient = D("Patient")->queryById($workorder["patient_id"]);
        $employeeList = D("Employee")->alias("e")
            ->join("left join __CUSTOMER__ c on c.customer_id = e.employee_id")
            ->field("e.*, c.openid")
            ->select();
        $openidArray = array();
        foreach ($employeeList as $singleEmployee) {
            $job = explode(',', $singleEmployee["job"]);
            if ($job[0] == "4" || $job[0] == "100") {//临床部协调人
                array_push($openidArray, $singleEmployee["openid"]);
            }
//            if ($singleEmployee["employee_id"] == $workorder["sales_id"] && !in_array($singleEmployee["openid"], $openidArray)) {
//                array_push($openidArray, $singleEmployee["openid"]);
//            }
        }
        foreach ($openidArray as $openid) {
            $notifyData["openid"] = $openid;
            $notifyData["url"] = "http://xwkang.com/wechat/workorder/index/id/" . $workorder["workorder_id"];
            $notifyData["first"] = "收到新的工单：" . $workorder["workorder_no"] . "，请指派检测评估矫形师。";
            $notifyData["keyword1"] = "[诊断--" . $patient["diagnosis"] . "]";
            $notifyData["keyword2"] = $workorder["dispatch_time"];
            $notifyData["keyword3"] = $processor["employee_name"];
            $notifyData["remark"] = "患者基本信息：" . $patient["patient_name"] . "，" . $patient["sex"] . "，" . $patient["age"] . "岁。 销售：" . $sales["employee_name"];
            WorkorderCreateNotify($notifyData);
        }

        echo json_encode($data);
    }
    //略过此功能
    public function takeOrder()
    {
        $workorder = D("Workorder")->queryById(I("post.workorderId"));
        $workorder["take_time"] = getCurrentDate();

        if(I("post.examinatorId") == -1){
            $workorder["exam_time"] = $workorder["update_time"];
            $workorder["examinator_id"] = $workorder["creator_id"];
        }else{
            $workorder["examinator_id"] = I("post.examinatorId");
        }

        if(I("post.prescriberId") == -1){
            $workorder["prescriber_id"] = $workorder["creator_id"];
        }else{
            $workorder["prescriber_id"] = I("post.prescriberId");
        }


        D("Workorder")->updateInfo($workorder);
        $data["code"] = 200;
//        $data["alert"] = "examinator_id".$workorder["examinator_id"];

        if(I("post.examinatorId") != -1) {
            //推送信息到接单人
            $processor = D("Employee")->queryById($this->customer["customer_id"]);
            $sales = D("Employee")->queryById($workorder["sales_id"]);
            $patient = D("Patient")->queryById($workorder["patient_id"]);
            $employeeList = D("Employee")->alias("e")
                ->join("left join __CUSTOMER__ c on c.customer_id = e.employee_id")
                ->field("e.*, c.openid")
                ->select();
            $openidArray = array();
            foreach ($employeeList as $singleEmployee) {
//            $job = explode(',', $singleEmployee["job"]);
//            if ($job[1] == "2") {
//                array_push($openidArray, $singleEmployee["openid"]);
//            }
                if ($singleEmployee["employee_id"] == $workorder["examinator_id"] && !in_array($singleEmployee["openid"], $openidArray)) {
                    array_push($openidArray, $singleEmployee["openid"]);
                }
            }
            foreach ($openidArray as $openid) {
                $notifyData["openid"] = $openid;
                $notifyData["url"] = "http://xwkang.com/wechat/workorder/index/id/" . $workorder["workorder_id"];
                $notifyData["first"] = "收到新的工单：" . $workorder["workorder_no"] . "，请填写检测评估数据。";
                $notifyData["keyword1"] = "[诊断--" . $patient["diagnosis"] . "]";
                $notifyData["keyword2"] = $workorder["take_time"];
                $notifyData["keyword3"] = $processor["employee_name"];
                $notifyData["remark"] = "患者基本信息：" . $patient["patient_name"] . "，" . $patient["sex"] . "，" . $patient["age"] . "岁。 销售：" . $sales["employee_name"];
                WorkorderCreateNotify($notifyData);
            }
        }
        echo json_encode($data);
    }
    //略过此功能
    public function examOrder()
    {
        $workorder = D("Workorder")->queryById(I("post.workorderId"));
        $workorder["exam_time"] = getCurrentDate();


        D("Workorder")->updateInfo($workorder);
        $data["code"] = 200;
//        $data["alert"] = "mobility".$examBasicInfo["mobility"];

        //推送信息到接单人
        $processor = D("Employee")->queryById($this->customer["customer_id"]);
        $sales = D("Employee")->queryById($workorder["sales_id"]);
        $patient = D("Patient")->queryById($workorder["patient_id"]);
        $employeeList = D("Employee")->alias("e")
            ->join("left join __CUSTOMER__ c on c.customer_id = e.employee_id")
            ->field("e.*, c.openid")
            ->select();
        $openidArray = array();
        foreach ($employeeList as $singleEmployee) {
            $job = explode(',', $singleEmployee["job"]);
            if ($job[0] == "4" || $job[0] == "100") {//临床部协调人
                array_push($openidArray, $singleEmployee["openid"]);
            }
//            if ($singleEmployee["employee_id"] == $workorder["sales_id"] && !in_array($singleEmployee["openid"], $openidArray)) {
//                array_push($openidArray, $singleEmployee["openid"]);
//            }
        }
        foreach ($openidArray as $openid) {
            $notifyData["openid"] = $openid;
            $notifyData["url"] = "http://xwkang.com/wechat/workorder/index/id/" . $workorder["workorder_id"];
            $notifyData["first"] = "工单：" . $workorder["workorder_no"] . " 检测评估信息已提交。";
            $notifyData["keyword1"] = "[诊断--" . $patient["diagnosis"] . "]";
            $notifyData["keyword2"] = $workorder["exam_time"] . " - " . $processor["employee_name"];
            $notifyData["remark"] = "患者基本信息：" . $patient["patient_name"] . "，" . $patient["sex"] . "，" . $patient["age"] . "岁。 销售：" . $sales["employee_name"];
            WorkorderChangeNotify($notifyData);
        }

        echo json_encode($data);
    }
    public function submitOrder()
    {
        $workorder = D("Workorder")->queryById(I("post.workorderId"));

        $workorder["submit_time"] = getCurrentDate();
        $workorder["type_name"] = I("post.typeName");
        $workorder["deposit"] = I("post.deposit");
        $workorder["price"] = I("post.price");
        $workorder["delivery_date"] = I("post.deliveryDate");
        $workorder["adapt"] = I("post.adapt");
        $workorder["status"] = 3;

        $prescriberChanged = -1;
        if($workorder["prescriber_id"] != I("post.prescriberId")){
            $prescriberChanged = 1;
            if(I("post.prescriberId") == -1){
                $workorder["prescriber_id"] = $workorder["creator_id"];
            }else{
                $workorder["prescriber_id"] = I("post.prescriberId");
            }

        }


        D("Workorder")->updateInfo($workorder);
        $data["code"] = 200;
//        $data["alert"] = "mobility".$examBasicInfo["mobility"];

        //推送信息到接单人
        $processor = D("Employee")->queryById($this->customer["customer_id"]);
        $sales = D("Employee")->queryById($workorder["sales_id"]);
        $patient = D("Patient")->queryById($workorder["patient_id"]);
        $prescriber = D("Employee")->queryById($workorder["prescriber_id"]);
        $employeeList = D("Employee")->alias("e")
            ->join("left join __CUSTOMER__ c on c.customer_id = e.employee_id")
            ->field("e.*, c.openid")
            ->select();
        $openidArray = array();
        foreach ($employeeList as $singleEmployee) {
            $job = explode(',', $singleEmployee["job"]);
            if (($job[0] == "3" //客服
                || $job[2] == "2"//库管
                || $job[0] == "100")
                && !in_array($singleEmployee["openid"], $openidArray)) {
                array_push($openidArray, $singleEmployee["openid"]);
            }
            if (($singleEmployee["employee_id"] == $workorder["creator_id"] //下单人
                    || $singleEmployee["employee_id"] == $workorder["sales_id"]) //销售
                    && !in_array($singleEmployee["openid"], $openidArray)) {
                array_push($openidArray, $singleEmployee["openid"]);
            }
        }
        foreach ($openidArray as $openid) {
            $notifyData["openid"] = $openid;
            $notifyData["url"] = "http://xwkang.com/wechat/workorder/index/id/" . $workorder["workorder_id"];
            $notifyData["first"] = "工单：已接单。矫形器种类：" . $workorder["type_name"] ."，订单价格：" . $workorder["price"] ."，已收预付款：" . $workorder["deposit"] ."，预计交付时间：" . $workorder["delivery_date"];
            if($prescriberChanged == 1){
                $notifyData["first"]  = $notifyData["first"] .  "。** 处方人变更为：" . $prescriber["employee_name"] ." **";
            }
            $notifyData["keyword1"] = $workorder["workorder_no"];
            $notifyData["keyword2"] = "[诊断--" . $patient["diagnosis"] . "]";
            $notifyData["keyword3"] = $workorder["submit_time"] . " - " . $processor["employee_name"];
            $notifyData["remark"] = "患者基本信息：" . $patient["patient_name"] . "，" . $patient["sex"] . "，" . $patient["age"] . "岁。 销售：" . $sales["employee_name"] . "。处方人：" . $prescriber["employee_name"];
            WorkorderProcessingNotify($notifyData);
        }

        echo json_encode($data);
    }

    public function changeStatus()
    {
        $workorder = D("Workorder")->queryById(I("post.workorderId"));

        $workorder["type_name"] = I("post.typeName");
        $workorder["sales_id"] = I("post.salesId");
        $workorder["examinator_id"] = I("post.examinatorId");
        $workorder["prescriber_id"] = I("post.prescriberId");
        $workorder["deposit"] = I("post.deposit");
        $workorder["price"] = I("post.price");
        $workorder["adapt"] = I("post.adapt");
        $workorder["delivery_date"] = I("post.deliveryDate");
        $workorder["adapt"] = I("post.adapt");
        $workorder["status"] = I("post.statusId");

        D("Workorder")->updateInfo($workorder);
        $data["code"] = 200;
//        $data["alert"] = "mobility".$examBasicInfo["mobility"];

        echo json_encode($data);
    }

    public function closeOrder()
    {
        $workorder = D("Workorder")->queryById(I("post.workorderId"));
        $workorder["close_reason"] = I("post.closeReason");
        $workorder["close_time"] = getCurrentDate();
        $workorder["status"] = -2;

        D("Workorder")->updateInfo($workorder);
        $data["code"] = 200;
//        $data["alert"] = "mobility".$examBasicInfo["mobility"];

        //推送信息到接单人
        $processor = D("Employee")->queryById($this->customer["customer_id"]);
        $sales = D("Employee")->queryById($workorder["sales_id"]);
        $patient = D("Patient")->queryById($workorder["patient_id"]);
        $employeeList = D("Employee")->alias("e")
            ->join("left join __CUSTOMER__ c on c.customer_id = e.employee_id")
            ->field("e.*, c.openid")
            ->select();
        $openidArray = array();
        foreach ($employeeList as $singleEmployee) {
            $job = explode(',', $singleEmployee["job"]);
            if ($job[0] == "3" || $job[0] == "100") {//客服
                array_push($openidArray, $singleEmployee["openid"]);
            }
            if (($singleEmployee["employee_id"] == $workorder["creator_id"] //下单人
                    || $singleEmployee["employee_id"] == $workorder["sales_id"] //销售
                    || $singleEmployee["employee_id"] == $workorder["examinator_id"]) //检测评估人
                && !in_array($singleEmployee["openid"], $openidArray)) {
                array_push($openidArray, $singleEmployee["openid"]);
            }
        }
        foreach ($openidArray as $openid) {
            $notifyData["openid"] = $openid;
            $notifyData["url"] = "http://xwkang.com/wechat/workorder/index/id/" . $workorder["workorder_id"];
            $notifyData["first"] = "工单：" . $workorder["workorder_no"] . " 已关闭，关闭原因：" . $workorder["close_reason"];
            $notifyData["keyword1"] = "[诊断--" . $patient["diagnosis"] . "]";
            $notifyData["keyword2"] = $workorder["close_time"] . " - " . $processor["employee_name"];
            $notifyData["remark"] = "患者基本信息：" . $patient["patient_name"] . "，" . $patient["sex"] . "，" . $patient["age"] . "岁。 销售：" . $sales["employee_name"];
            WorkorderChangeNotify($notifyData);
        }

        echo json_encode($data);
    }

    public function storageOrder()
    {
        $workorder = D("Workorder")->queryById(I("post.workorderId"));
        $workorder["storage_time"] = getCurrentDate();


        D("Workorder")->updateInfo($workorder);
        $data["code"] = 200;
//        $data["alert"] = "mobility".$examBasicInfo["mobility"];

        //推送信息到接单人
        $processor = D("Employee")->queryById($this->customer["customer_id"]);
        $sales = D("Employee")->queryById($workorder["sales_id"]);
        $patient = D("Patient")->queryById($workorder["patient_id"]);
        $prescriber = D("Employee")->queryById($workorder["prescriber_id"]);
        $employeeList = D("Employee")->alias("e")
            ->join("left join __CUSTOMER__ c on c.customer_id = e.employee_id")
            ->field("e.*, c.openid")
            ->select();
        $openidArray = array();
        foreach ($employeeList as $singleEmployee) {
            $job = explode(',', $singleEmployee["job"]);
            if ($job[0] == "3" || $job[0] == "100") {//客服
                array_push($openidArray, $singleEmployee["openid"]);
            }
//            if ($singleEmployee["employee_id"] == $workorder["sales_id"] && !in_array($singleEmployee["openid"], $openidArray)) {
//                array_push($openidArray, $singleEmployee["openid"]);
//            }
        }
        foreach ($openidArray as $openid) {
            $notifyData["openid"] = $openid;
            $notifyData["url"] = "http://xwkang.com/wechat/workorder/index/id/" . $workorder["workorder_id"];
            $notifyData["first"] = "工单：" . $workorder["workorder_no"] . " 已入库，可以发货。矫形器种类：" . $workorder["type_name"] ."，订单价格：" . $workorder["price"];
            $notifyData["keyword1"] = "[诊断--" . $patient["diagnosis"] . "]";
            $notifyData["keyword2"] = $workorder["storage_time"] . " - " . $processor["employee_name"];
            $notifyData["remark"] = "患者基本信息：" . $patient["patient_name"] . "，" . $patient["sex"] . "，" . $patient["age"] . "岁。 销售：" . $sales["employee_name"]. "。处方人：" . $prescriber["employee_name"];
            WorkorderChangeNotify($notifyData);
        }

        echo json_encode($data);
    }
    public function endOrder()
    {
        $workorder = D("Workorder")->queryById(I("post.workorderId"));

        $workorder["end_time"] = getCurrentDate();
        $workorder["status"] = 4;

        D("Workorder")->updateInfo($workorder);
        $data["code"] = 200;
//        $data["alert"] = "mobility".$examBasicInfo["mobility"];

        //推送信息到接单人
        $processor = D("Employee")->queryById($this->customer["customer_id"]);
        $creator = D("Employee")->queryById($workorder["creator_id"]);
        if($workorder["examinator_id"] == -1){
            $examinator = $creator;
        }else{
            $examinator = D("Employee")->queryById($workorder["examinator_id"]);
        }

        $sales = D("Employee")->queryById($workorder["sales_id"]);
        $prescriber = D("Employee")->queryById($workorder["prescriber_id"]);
        $patient = D("Patient")->queryById($workorder["patient_id"]);
        $employeeList = D("Employee")->alias("e")
            ->join("left join __CUSTOMER__ c on c.customer_id = e.employee_id")
            ->field("e.*, c.openid")
            ->select();
        $openidArray = array();
        foreach ($employeeList as $singleEmployee) {
            $job = explode(',', $singleEmployee["job"]);
            if ($job[0] == "100") {
                array_push($openidArray, $singleEmployee["openid"]);
            }
            if (($singleEmployee["employee_id"] == $workorder["creator_id"] //下单人
                    || $singleEmployee["employee_id"] == $workorder["sales_id"]) //销售
                && !in_array($singleEmployee["openid"], $openidArray)) {
                array_push($openidArray, $singleEmployee["openid"]);
            }
        }
        foreach ($openidArray as $openid) {
            $notifyData["openid"] = $openid;
            $notifyData["url"] = "http://xwkang.com/wechat/workorder/index/id/" . $workorder["workorder_id"];
            if($workorder["workorder_type"] == 1){
                $notifyData["first"] = "DRAFO工单已交付,工单号：" . $workorder["workorder_no"];
            }
            if($workorder["workorder_type"] == 2){
                $notifyData["first"] = "脊柱工单已交付,工单号：" . $workorder["workorder_no"];
            }
            $notifyData["keyword1"] = "[诊断--" . $patient["diagnosis"] . "]";
            $notifyData["keyword2"] = "亦庄临床部。矫形器种类：" . $workorder["type_name"] ."，订单价格：" . $workorder["price"];
            $notifyData["keyword3"] = $workorder["update_time"] . "（下单时间）";
            $notifyData["keyword4"] = $workorder["end_time"]. "（发货时间）";
            $notifyData["keyword5"] = "下单人：" . $creator["employee_name"] . "，检测人：" . $examinator["employee_name"];
            $notifyData["remark"] = "患者基本信息：" . $patient["patient_name"] . "，" . $patient["sex"] . "，" . $patient["age"] . "岁。 销售：" . $sales["employee_name"]. "。处方人：" . $prescriber["employee_name"];
            workorderEndNotify($notifyData);
        }

        echo json_encode($data);
    }

}